package com.example.e_comm.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.e_comm.entity.Permission;
import com.example.e_comm.entity.Role;
import com.example.e_comm.service.PermissionService;
import com.example.e_comm.service.RoleService;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/roles")
public class RoleController {
    private RoleService roleService;
    private PermissionService permissionService;
    public RoleController(RoleService _roleService,PermissionService _permissionService) {
        this.roleService = _roleService;
        this.permissionService = _permissionService;
    }
    @RequestMapping("/list")
    public List<Role> list() {
        return roleService.list();
    }
    @GetMapping("/page")
    public IPage<Role> page(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize
    ){
        return roleService.page(new Page<>(pageNum,pageSize));
    }

    public RoleService getRoleService() {
        return roleService;
    }

    public void setRoleService(RoleService roleService) {
        this.roleService = roleService;
    }

    public PermissionService getPermissionService() {
        return permissionService;
    }

    public void setPermissionService(PermissionService permissionService) {
        this.permissionService = permissionService;
    }

    //    @GetMapping("/search")
//    public IPage<Role>search(
//            @RequestParam(defaultValue = "1") Integer pageNum,
//            @RequestParam(defaultValue = "10") Integer pageSize,
//            @RequestParam(required = false) String keyword
//    ){
//        Page<Role> page = new Page<>(pageNum,pageSize);
//        return roleService.search( page,new LambdaQueryChainWrapper<Role>()
//                .like(Role::getName,keyword)
//                .or()
//                .like(Role::getDescription,keyword)
//                .or()
//                .like(Role::getRoleKey,keyword)
//        );
//    }
    @GetMapping("/search")
    public IPage<Role> search(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize,
            @RequestParam(required = false) String keyword
    ) {
        Page<Role> page = new Page<>(pageNum, pageSize);
        return new LambdaQueryChainWrapper<Role>(roleService.getBaseMapper())
                .like(keyword != null && !keyword.isEmpty(), Role::getName, keyword)
                .or(keyword != null && !keyword.isEmpty())
                .like(keyword != null && !keyword.isEmpty(), Role::getDescription, keyword)
                .or(keyword != null && !keyword.isEmpty())
                .like(keyword != null && !keyword.isEmpty(), Role::getRoleKey, keyword)
                .page(page);
    }


    @PostMapping("/create")
    public Role createRole(@RequestBody Role entity){
        //检查角色是否存在
        if (roleService.getById(entity.getName())!=null){
            throw new IllegalArgumentException("角色已存在");
        }
        roleService.save(entity);
        return entity;
    }
    @PostMapping("/update")
    public Role updateRole(@RequestBody Role entity){
        //检查角色是否存在
        if (roleService.getById(entity.getId())==null){
            throw new IllegalArgumentException("角色已存在");
        }
        roleService.updateById(entity);
        return entity;
    }
@PostMapping("/delete/{roleId}")
public void deleteRole(@PathVariable Long roleId){
    // 检查角色是否存在
    if (roleService.getById(roleId) == null) {
        throw new IllegalArgumentException("角色不存在！roleId: " + roleId);
    }
    roleService.removeById(roleId);
}

    @PostMapping("/deleteBatch")
    public void deleteRole(@PathVariable List<Long> roleIds){
        //检查角色是否存在
        List<Role> roles = roleService.listByIds(roleIds);
        if (roles.size()!=roleIds.size()){
            throw new IllegalArgumentException("角色有误，请核查");
        }
        roleService.removeByIds(roleIds);
    }
    //获取角色权限列表
    @GetMapping("/getPermission")
    public List<Permission> getPermissionByRoleId(Long roleId){
        return permissionService.getPermissionsByRoleId(roleId);
    }
    //为角色分配权限
    @PostMapping("/assignPermissions")
    public void assignPermissions(
            @RequestParam Long roleId,
            @RequestParam List<Long> permissionIds
    ){
            Role role = roleService.getById(roleId);
            if (role==null){
                throw new IllegalArgumentException("角色不存在");
            }
            List<Permission> permissions = permissionService.listByIds(permissionIds);
            if (permissions.size()!=permissionIds.size()){
                throw new IllegalArgumentException("请核查权限后再修改");
            }
            roleService.assignPermissions(roleId,permissionIds);
        }
    
}
